package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Shop;
import com.ruoyi.system.mapper.ShopMapper;
import com.ruoyi.system.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 门店信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-20
 */
@Service
public class ShopServiceImpl implements IShopService
{
    @Autowired
    private ShopMapper shopMapper;

    /**
     * 查询门店信息
     *
     * @param id 门店信息主键
     * @return 门店信息
     */
    @Override
    public Shop selectShopById(Long id)
    {
        return shopMapper.selectShopById(id);
    }

    /**
     * 查询门店信息列表
     *
     * @param shop 门店信息
     * @return 门店信息
     */
    @Override
    public List<Shop> selectShopList(Shop shop)
    {
        shop.setOwnerId(SecurityUtils.getUserId());
        String roles = shop.getRoles();
        if (StringUtils.isNotBlank(roles)){
            /**只管控店长*/
            if (roles.contains(UserConstants.SHOP_OWNER)){
                shop.setRoles(UserConstants.SHOP_OWNER);
            }else {
                shop.setRoles(null);
            }
        }
        return shopMapper.selectShopList(shop);
    }

    /**
     * 新增门店信息
     *
     * @param shop 门店信息
     * @return 结果
     */
    @Override
    public int insertShop(Shop shop)
    {
        if (Objects.isNull(shop.getOwnerId())){
            shop.setOwnerId(SecurityUtils.getUserId());
        }
        return shopMapper.insertShop(shop);
    }

    /**
     * 修改门店信息
     *
     * @param shop 门店信息
     * @return 结果
     */
    @Override
    public int updateShop(Shop shop)
    {
        return shopMapper.updateShop(shop);
    }

    /**
     * 批量删除门店信息
     *
     * @param ids 需要删除的门店信息主键
     * @return 结果
     */
    @Override
    public int deleteShopByIds(Long[] ids)
    {
        return shopMapper.deleteShopByIds(ids);
    }

    /**
     * 删除门店信息信息
     *
     * @param id 门店信息主键
     * @return 结果
     */
    @Override
    public int deleteShopById(Long id)
    {
        return shopMapper.deleteShopById(id);
    }
}
